package cn.zjj.system.service.impl;

import cn.zjj.basic.service.impl.BaseServiceImpl;
import cn.zjj.system.domain.SystemDetail;
import cn.zjj.system.domain.SystemType;
import cn.zjj.system.mapper.SystemDetailMapper;
import cn.zjj.system.mapper.SystemTypeMapper;
import cn.zjj.system.service.ISystemTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SystemTypeServiceImpl extends BaseServiceImpl<SystemType> implements ISystemTypeService {
    @Autowired
    private SystemTypeMapper systemTypeMapper;

    @Autowired
    private SystemDetailMapper systemDetailMapper;


    @Transactional
    @Override
    public void delete(Long id) {

        methodToDel(id);

        /*try {
            List<SystemDetail> systemDetails = systemDetailMapper.selectByTypeId(id);
            if(systemDetails!=null){
                ArrayList<Long> ids = new ArrayList<>();
                for (SystemDetail detail : systemDetails) {
                    Long did= detail.getId();
                    ids.add(did);
                }
                systemDetailMapper.patchDel(ids);
            }
        } catch (Exception e) {

        }finally {

        }*/


        systemTypeMapper.delete(id);

    }

    @Transactional
    @Override
    public void patchDel(List<Long> ids) {
        for (Long id : ids) {
            /*try {
                List<SystemDetail> systemDetails = systemDetailMapper.selectByTypeId(id);
                if(systemDetails!=null){
                    ArrayList<Long> dids = new ArrayList<>();
                    for (SystemDetail detail : systemDetails) {
                        Long did= detail.getId();
                        dids.add(did);
                    }

                    systemDetailMapper.patchDel(dids);
                }
            } catch (Exception e) {}*/

            methodToDel(id);

        }

        systemTypeMapper.patchDel(ids);
    }


    private  void methodToDel(Long id){
        try {
            List<SystemDetail> systemDetails = systemDetailMapper.selectByTypeId(id);
            if(systemDetails!=null){
                ArrayList<Long> ids = new ArrayList<>();
                for (SystemDetail detail : systemDetails) {
                    Long did= detail.getId();
                    ids.add(did);
                }
                systemDetailMapper.patchDel(ids);
            }
        } catch (Exception e) {

        }
    }
}
